package com.zh.onlineexam.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zh.onlineexam.dto.TInfoTextContentDto;
import com.zh.onlineexam.entity.TInfoTextContent;
import com.zh.onlineexam.listener.TInfoTextContentReadListener;
import com.zh.onlineexam.service.TInfoTextContentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @className InfoTextContentController
 * @Description TODO
 * @Auther 29934
 * @Date 2022/8/20 10:42
 * @Version 1.0
 **/
@RestController
@RequestMapping("/tInfoTextContent")
@Slf4j
@RequiredArgsConstructor
public class InfoTextContentController {

    private final TInfoTextContentService tInfoTextContentService;

    @RequestMapping("/listAll/{subject}/{level}")
    public List<TInfoTextContent> listTInfoTextContent(@PathVariable("subject") Integer subjectId
            ,@PathVariable("level") Integer level){
        return tInfoTextContentService.list(new QueryWrapper<TInfoTextContent>()
                .eq("subject_id", subjectId).eq("level",level));
    }

    /**
     * excel导入数据
     * @param file
     * @throws IOException
     */
    @RequestMapping("/import")
    public boolean listTInfoTextContent(@RequestParam("file") MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), TInfoTextContentDto.class, new TInfoTextContentReadListener()).sheet().doRead();
        return tInfoTextContentService.saveImportData();
    }
}
